﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(I18NFont))]
    public class I18NFontEditor : Editor
    {
        private SerializedProperty _prop;
        private ReorderableList reordelist;
		public override void OnInspectorGUI()
		{
			serializedObject.Update();
			EditorGUILayout.Space(5f);
			EditorGUILayout.HelpBox("各国字体库下载地址：\n\n https://fonts.google.com/\n", MessageType.Info);
			EditorGUILayout.Space(10f);
			this.reordelist.DoLayoutList();
			serializedObject.ApplyModifiedProperties();
		}

		private void OnEnable()
		{
			if (this.target == null) return;
			this._prop = serializedObject.FindProperty("entries");
			reordelist = new ReorderableList(serializedObject, this._prop);
			reordelist.drawElementCallback = DrawNameElement;
			reordelist.drawHeaderCallback = DrawHeaderCallback;
			reordelist.elementHeight = 3.5f * EditorGUIUtility.singleLineHeight;
		}
		private static void DrawHeaderCallback(Rect rect)
		{
			GUI.Label(rect, "Font Type:");
		}

        private void DrawNameElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            var element = this._prop.GetArrayElementAtIndex(index);
            var Language = element.FindPropertyRelative("Language");
			var fontname = element.FindPropertyRelative("fontname");
			var font = element.FindPropertyRelative("font");

            Rect _rect = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
			EditorGUI.PropertyField(_rect, Language, new GUIContent("Language:"));
			_rect.y += 1.1f * EditorGUIUtility.singleLineHeight;
			EditorGUI.PropertyField(_rect, fontname, new GUIContent("FontName:"));
			_rect.y += 1.1f * EditorGUIUtility.singleLineHeight;
			EditorGUI.BeginChangeCheck();
			EditorGUI.PropertyField(_rect, font, new GUIContent("Font:"));
			if (EditorGUI.EndChangeCheck())
            {
				fontname.stringValue = font.objectReferenceValue != null? font.objectReferenceValue.name:"";
			}
		}

    }
}

